<template>
  <div>
    <!-- 点击编辑弹窗组件 -->
    <el-dialog
      :title="ruleForm.id?'编辑经纪':'添加经纪'"
      width="60%"
      :visible="visagents"
      @close="cancelBtn"
    >
      <div class="content">
        <el-form ref="ruleForm" :rules="rules" :model="ruleForm" label-width="100px" class="demo-ruleForm">

          <el-form-item label="名称" prop="name">
            <el-input v-model="ruleForm.name" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="ruleForm.email" />
          </el-form-item>
          <el-form-item label="电话" prop="phone">
            <el-input v-model="ruleForm.phone" />
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="ruleForm.address" />
          </el-form-item>
          <el-form-item label="描述" prop="desc">
            <el-input v-model="ruleForm.desc" type="textarea" />
          </el-form-item>
          <el-form-item label="封面">
            <Upload ref="upload" />
          </el-form-item>
        </el-form>

      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmBtn">确 定</el-button>
        <el-button @click="cancelBtn">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
// 获取指定经纪人
// 编辑经纪人
import { getAgent, putAgent, postAagent } from '@/api/agents'
export default {
  props: {
    visagents: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 表单数据
      ruleForm: {
        name: '', // 名字
        desc: '', // 描述
        phone: '', // 电话
        address: '', // 地址
        email: '', // 邮箱
        image: '' // 封面

      },
      Agentid: 0, // 经纪人id
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }

        ],
        desc: [
          { required: true, message: '请输入描述', trigger: 'blur' }

        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' }

        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' }

        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }

        ]
      }
    }
  },
  methods: {
    // 确定按钮
    confirmBtn() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            // 编辑
            await putAgent({ ...this.ruleForm, image: this.$refs.upload.imageUrl })
            // 提示
            this.$message({
              message: '编辑成功',
              type: 'success'
            })
          } else {
            await postAagent({ ...this.ruleForm, image: this.$refs.upload.imageUrl })
            // 提示
            this.$message({
              message: '添加成功',
              type: 'success'
            })
          }

          // 关闭弹窗
          this.cancelBtn()
          // 渲染
          this.$emit('getAgents')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 获取经纪详情
    async  getAgentdetail(id) {
      const res = await getAgent(id)
      this.ruleForm = res.data
      this.$refs.upload.imageUrl = res.data.image
    },
    // 取消按钮
    cancelBtn() {
      this.ruleForm = {
        name: '', // 名字
        desc: '', // 描述
        phone: '', // 电话
        address: '', // 地址
        email: '', // 邮箱
        image: '' // 封面

      }
      this.$refs.upload.imageUrl = ''
      this.$refs.ruleForm.resetFields()
      this.$emit('update:visagents', false)
    }
  }
}
</script>
    <style>
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .content{
    height: 400px;
    overflow: auto;
  }
    </style>

